/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.usfirst.frc675.Fred;

import com.sun.squawk.io.BufferedReader;
import com.sun.squawk.microedition.io.FileConnection;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.io.Connector;

/**
 *
 * @author derrickNB
 */
public class CSVFileReader {
        private DoubleArray1d rows;
    private DoubleArray1d columns;
    private DoubleArray2d values;

    public void ReadFile(String filename) {

        rows = new DoubleArray1d();
        columns = new DoubleArray1d();
        values = new DoubleArray2d();
        boolean error = false;

        FileConnection fc =null;
        InputStreamReader isr = null;
        System.out.println("Reading File : "+filename);
        
        try {
            fc = (FileConnection) Connector.open(filename);
            isr = new InputStreamReader (fc.openDataInputStream());
        } catch (IOException e) {
            error = true;
        }
        if (error) {
            {
                System.out.println("Cannot Open this file: "+filename);
                return;
            }
        }
        BufferedReader reader = new BufferedReader(isr);

        String dataRow = null;

        try {
            dataRow = reader.readLine();
        } catch (IOException e) {
            error = true;
        }
        if (error || dataRow == null) {
            {
                return;
            }
        }
        //System.out.println(dataRow);

        String[] fields = split(dataRow,',');

        for (int i =0; i<fields.length; i++) {
            String s = fields [i];
            if (!s.equals("")) { //Assuming the first value is blank --TOSS Value--
                double d = Double.parseDouble(s);
                columns.add(d);
            }
        }

        //System.out.println(columns);

        boolean done = false;
        while (!done) {
            try {
                dataRow = reader.readLine();
            } catch (IOException e) {
                error = true;
            }
            if (error || dataRow == null) {
                done = true;
            } else {
                DoubleArray1d thisRow = new DoubleArray1d();
               // System.out.println(dataRow);
                String[] rowFields = split(dataRow,',');
                for (int i =0; i<rowFields.length; i++) 
                {
                    String s= rowFields[i];
                    double d = Double.parseDouble(s);
                    thisRow.add(d);
                }
                //System.out.println(thisRow);
                double d = thisRow.get(0);
                rows.add(d);
                thisRow.remove(0);// remove the first element of the row
                values.add(thisRow);
            }

        }
        //System.out.println("================");
        //System.out.println(rows);
        //System.out.println("================");
        //System.out.println(values);
        try {
            reader.close();
            isr.close();
            fc.close();
        } catch (IOException e) {
            //do nothing
        }
    }
        
        public String[] split(String s, char c){
            Vector v = new Vector();
            int iStart = 0;
            while (iStart< s.length()){
            int iEnd = s.indexOf(c,iStart);
            if(iEnd == -1){
                iEnd = s.length();
            }
            String token = s.substring(iStart, iEnd);
            v.addElement(token);
            iStart = iEnd +1;
        }
            String result[] = new String[v.size()];
            for (int i = 0; i< v.size(); i++){
                result[i] = (String) v.elementAt(i);
            }
            return result;
            
    }
    public DoubleArray1d getRows(){
        return rows;    
    }
    public DoubleArray1d getColumns(){
        return columns;    
    }
    public DoubleArray2d getValues(){
        return values;    
    }
        
}

